# -*- coding: utf-8 -*-
import PySimpleGUI as sg
import spire.xls as excel
# All the stuff inside your window.



def remove_all_chars(s, char_to_remove):
    return s.replace(char_to_remove, '')

char_to_remove='<h2 style="color:red">Evaluation&nbsp;Warning&nbsp;:&nbsp;The&nbsp;document&nbsp;was&nbsp;created&nbsp;with&nbsp;&nbsp;Spire.XLS&nbsp;for&nbsp;Python</h2>'

layout = [
[sg.Text('by火鸡科学家')],
    [sg.HorizontalSeparator()],
    [sg.InputText('所选文件路径', size=(60, 2),key='-FILEPATH-')],
    [sg.FileBrowse("点击请选择排班excel文件",target='-GETFILE-',key='-GETFILE-',
         enable_events=True,font=16,size=(60,2))],
            [sg.Button('生成',font=16,size=(10,2),key='-GENERATE-'), sg.Button('退出',font=16,size=(10,2))],
[sg.HorizontalSeparator()],
    [sg.Multiline('', size=(700, 400), key='-OUTPUT-')],

]

# Create the Window
window = sg.Window('Excel转Html小工具', layout,size=(800,600))


# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == '退出': # if user closes window or clicks cancel
        break
    elif event=='-GETFILE-':
        name = values['-GETFILE-']
        window['-FILEPATH-'].update(name)
    elif event == '-GENERATE-':
        workbook = excel.Workbook()
        if not (values['-FILEPATH-'].endswith('.xls') or values['-FILEPATH-'].endswith('.xlsx')):
            window['-FILEPATH-'].update('请选择Excel文件！！！！')
            continue
        workbook.LoadFromFile(values['-FILEPATH-'])
        sheet = workbook.Worksheets.get_Item(0)
        # 转换为HTML并保留样式
        html_options = excel.HTMLOptions()
        # 保存为HTML
        sheet.SaveToHtml("output.html", html_options)
        workbook.Dispose()
        with open('output.html', 'r',encoding='UTF-8') as file:
            html_content = file.read()
            ready_to_paste=remove_all_chars(html_content, char_to_remove)
        window['-OUTPUT-'].update(ready_to_paste)

window.close()



